package com.example.ftp4j_demo2;

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferListener;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import com.example.ftp4j_demo2.FtpMainActivity.DameonFtpConnector;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;



public class CmdFactory {
	
	private static final int MENU_OPTIONS_BASE = 0;
	private static final int MSG_CMD_CONNECT_OK = MENU_OPTIONS_BASE + 1;
	private static final int MSG_CMD_CONNECT_FAILED = MENU_OPTIONS_BASE + 2;
	private static final int MSG_CMD_LIST_OK = MENU_OPTIONS_BASE + 3;
	private static final int MSG_CMD_LIST_FAILED = MENU_OPTIONS_BASE + 4;
	private static final int MSG_CMD_CWD_OK = MENU_OPTIONS_BASE + 5;
	private static final int MSG_CMD_CWD_FAILED = MENU_OPTIONS_BASE + 6;
	private static final int MSG_CMD_DELE_OK = MENU_OPTIONS_BASE + 7;
	private static final int MSG_CMD_DELE_FAILED = MENU_OPTIONS_BASE + 8;
	private static final int MSG_CMD_RENAME_OK = MENU_OPTIONS_BASE + 9;
	private static final int MSG_CMD_RENAME_FAILED = MENU_OPTIONS_BASE + 10;

	private static final int MENU_OPTIONS_DOWNLOAD = MENU_OPTIONS_BASE + 20;
	private static final int MENU_OPTIONS_RENAME = MENU_OPTIONS_BASE + 21;
	private static final int MENU_OPTIONS_DELETE = MENU_OPTIONS_BASE + 22;
	private static final int MENU_DEFAULT_GROUP = 0;

	private static final int DIALOG_LOAD = MENU_OPTIONS_BASE + 40;
	private static final int DIALOG_RENAME = MENU_OPTIONS_BASE + 41;
	private static final int DIALOG_FTP_LOGIN = MENU_OPTIONS_BASE + 42;
	
	private String mCurrentPWD;
	private boolean mDameonRunning = true;
	private Object mLock = new Object();
	
		
	public FTPClient ftp;
		
	public CmdFactory(FTPClient ftp)
		
	{
		this.ftp = ftp;
	}

	public FtpCmd createCmdConnect() {
		return new CmdConnect(ftp);
	}

		public FtpCmd createCmdDisConnect() {
			return new CmdDisConnect(ftp);
		}

		public FtpCmd createCmdPWD() {
			return new CmdPWD(ftp);
		}

		public FtpCmd createCmdLIST() {
			return new CmdLIST(ftp);
		}

		public FtpCmd createCmdCWD(String path) {
			return new CmdCWD(path,ftp);
		}

		public FtpCmd createCmdDEL(String path, boolean isDirectory) {
			return new CmdDELE(path, isDirectory,ftp);
		}

	/*	public FtpCmd createCmdRENAME(String newPath) {
			return new CmdRENAME(newPath,ftp);
		}
*/



public abstract class FtpCmd implements Runnable {

	public abstract void run();
	public FTPClient mFTPClient;
	public FtpCmd(FTPClient ftp)
	{
		this.mFTPClient = ftp;
	}

}

public class CmdConnect extends FtpCmd {

	public CmdConnect(FTPClient ftp) {
		super(ftp);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		boolean errorAndRetry = false ;  //根据不同的异常类型，是否重新捕获
		try {
			String[] welcome = mFTPClient.connect(CONST.mFTPHost, CONST.mFTPPort);
			if (welcome != null) {
				for (String value : welcome) {
					logv("connect " + value);
				}
			}
			mFTPClient.login(CONST.mFTPUser, CONST.mFTPPassword);
			CONST.mHandler .sendEmptyMessage(MSG_CMD_CONNECT_OK);
		}catch (IllegalStateException illegalEx) {
			illegalEx.printStackTrace();
			errorAndRetry = true ;
		}catch (IOException ex) {
			ex.printStackTrace();
			errorAndRetry = true ;
		}catch (FTPIllegalReplyException e) {
			e.printStackTrace();
		}catch (FTPException e) {
			e.printStackTrace();
			errorAndRetry = true ;
		}
		if(errorAndRetry && mDameonRunning){
			CONST.mHandler .sendEmptyMessageDelayed(MSG_CMD_CONNECT_FAILED, 2000);
		}
	}
}

public class CmdDisConnect extends FtpCmd {

	public CmdDisConnect(FTPClient ftp) {
		super(ftp);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		if (mFTPClient != null) {
			try {
				mFTPClient.disconnect(true);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}
}

public class CmdPWD extends FtpCmd {

	public CmdPWD(FTPClient ftp) {
		super(ftp);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			String pwd = mFTPClient.currentDirectory();
			logv("pwd --- > " + pwd);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}

public class CmdLIST extends FtpCmd {

	public CmdLIST(FTPClient ftp) {
		super(ftp);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			mCurrentPWD = mFTPClient.currentDirectory();
			FTPFile[] ftpFiles = mFTPClient.list();
			logv(" Request Size  : " + ftpFiles.length);
			synchronized (mLock) {
				CONST.mFileList.clear();
				CONST.mFileList.addAll(Arrays.asList(ftpFiles));
			}
			CONST.mHandler .sendEmptyMessage(MSG_CMD_LIST_OK);

		} catch (Exception ex) {
			CONST.mHandler .sendEmptyMessage(MSG_CMD_LIST_FAILED);
			ex.printStackTrace();
		}
	}
}

public class CmdCWD extends FtpCmd {

	String realivePath;

	public CmdCWD(String path,FTPClient ftp) {
		super(ftp);
		realivePath = path;
	}

	@Override
	public void run() {
		try {
			mFTPClient.changeDirectory(realivePath);
			CONST.mHandler .sendEmptyMessage(MSG_CMD_CWD_OK);
		} catch (Exception ex) {
			CONST.mHandler .sendEmptyMessage(MSG_CMD_CWD_FAILED);
			ex.printStackTrace();
		}
	}
}

public class CmdDELE extends FtpCmd {

	String realivePath;
	boolean isDirectory;

	public CmdDELE(String path, boolean isDirectory,FTPClient ftp) {
		super(ftp);
		realivePath = path;
		this.isDirectory = isDirectory;
	}

	@Override
	public void run() {
		try {
			if (isDirectory) {
				mFTPClient.deleteDirectory(realivePath);
			} else {
				mFTPClient.deleteFile(realivePath);
			}
			CONST.mHandler .sendEmptyMessage(MSG_CMD_DELE_OK);
		} catch (Exception ex) {
			CONST.mHandler .sendEmptyMessage(MSG_CMD_DELE_FAILED);
			ex.printStackTrace();
		}
	}
}

/*public class CmdRENAME extends FtpCmd {

	String newPath;

	public CmdRENAME(String newPath , FTPClient ftp) {
		super(ftp);
		this.newPath = newPath;
	}

	@Override
	public void run() {
		try {
			mFTPClient.rename(mFileList.get(mSelectedPosistion).getName(),
					newPath);
			CONST.mHandler .sendEmptyMessage(MSG_CMD_RENAME_OK);
		} catch (Exception ex) {
			CONST.mHandler .sendEmptyMessage(MSG_CMD_RENAME_FAILED);
			ex.printStackTrace();
		}
	}
}
*/

private void logv(String log) {
	Log.v(CONST.TAG, log);
}



}
